#!/bin/sh
#!/bin/sh
PROJECT=alplinux
PRJROOT=~/${PROJECT}
mkdir -p  $PRJROOT
TARGET=arm-linux
PREFIX=${PRJROOT}/tools
TARGET_PREFIX=${PREFIX}/${TARGET}
export PATH=${PREFIX}/bin:${PATH}
KERNEL=linux-2.6.30.6
BINUTILS=binutils-2.20
GCC=gcc-4.4.3
KERNEL_LINK=ftp://ftp.kernel.org/pub/linux/kernel/v2.6/$KERNEL.tar.bz2
BINUTILS_LINK=http://ftp.gnu.org/gnu/binutils/$BINUTILS.tar.bz2
GCC_LINK=http://ftp.gnu.org/gnu/gcc/$GCC/$GCC.tar.bz2
kernel_fn()
{
	cd $PRJROOT
	#if kernel tar exist in the directory dont download
	if [ ! -e "$KERNEL.tar.bz2" ]; then
		cd $PRJROOT
		wget $KERNEL_LINK
	fi
	# dont untar if already exist 
	if [ ! -d "$KERNEL" ]; then
		cd $PRJROOT
		tar xvjf $KERNEL.tar.bz2
	fi

	#config if config or all or header argument 
	if [ "$1" = "config" ] || [ "$1" = "all" ] || [ "$1" = "header" ]; then
		cd $PRJROOT/$KERNEL
		make ARCH=arm CROSS_COMPILE=arm-linux- s3c2410_defconfig
		#you get error here becouse arm-linux-gcc not compiled yet
		make ARCH=arm CROSS_COMPILE=arm-linux- version
	fi
	# make if make or "all" argument 
	if [ "$1" = "make" ] || [ "$1" = "all" ]; then
		cd $PRJROOT/$KERNEL
		make ARCH=arm CROSS_COMPILE=arm-linux- zImage
	fi
	#kernel header copy
	if [ "$1" = "header" ] ; then
		cd $PRJROOT/$KERNEL
		mkdir -p ${TARGET_PREFIX}/include
		cp -r include/linux/ ${TARGET_PREFIX}/include
		cp -r include/asm-arm/ ${TARGET_PREFIX}/include/asm
		cp -r include/asm-generic/ ${TARGET_PREFIX}/include
	fi

}

binutils ()
{
	mkdir -p ${PRJROOT}/build-tools/build-binutils

	# download if file not exist 
	if [ ! -e "$BINUTILS.tar.bz2" ]; then
		cd ${PRJROOT}/build-tools
		wget $BINUTILS_LINK
	fi

	# untar if directory not exist 
	if [ ! -d "$BINUTILS" ]; then
		cd ${PRJROOT}/build-tools
		tar xvjf $BINUTILS.tar.bz2
	fi
	if [ "$1" = "make" ];then
		cd ${PRJROOT}/build-tools/build-binutils
		../$BINUTILS/configure --target=$TARGET --prefix=${PREFIX} 
		make
		make install
	fi
	
}

gcc_fn ()
{
	mkdir -p ${PRJROOT}/build-tools/build-boot-gcc

	# download if file not exist 
	if [ ! -e "$GCC.tar.bz2" ]; then
		cd ${PRJROOT}/build-tools
		wget $GCC_LINK
	fi

	# untar if directory not exist 
	if [ ! -d "$GCC" ]; then
		cd ${PRJROOT}/build-tools
		tar xvjf $GCC.tar.bz2
	fi
	if [ "$1" = "make" ];then
		cd ${PRJROOT}/build-tools/build-binutils
		
	fi
}

